//获取应用实例

var app = getApp()



Page({
	data: {
		navInfo: [{id: 1,name: "待付款",datatype: '0'},
			{id: 1,name: "待收货",datatype: '1'}, 
			{id: 2,name: "已收货",datatype: '2'}, 
			{id: 3,name: "已取消",datatype: '3'}
		],
		origalorderlist:[],/* 初始化订单列表数据 */
		orderlist:[],/* 订单列表数据 */
		orderItems:[],/* 订单详情 */
		payAmount:'',/* 订单总价全局变量 */
		coupon:'暂无优惠券',
		uid: 0,
		isoff: 0,
		type:'',/* 订单类型  */
		payindex:'',
		num:0,/* 默认页数 */
		bottomstatus:true/* 隐藏底部加载显示字体 */
	},
	goconfirmorder(e) {
		let id = e.currentTarget.dataset.type;
		if (id == 1) {
			wx.navigateTo({
				'url': '../confirmorder/confirmorder'
			})
		}

	},
	// 加载
	onLoad(options) {
		console.log(1);
		let that=this;
		let type = options.type;
		let num=that.data.num=0;
		that.data.type=type;
		that.typeorder(type,num)/* 加载订单数据 */
	},
	// 显示
	onShow() {
		console.log(0);
	},
	
	// 下拉刷新
	
	onPullDownRefresh() {
		
	},
	
	// 分享
	
	onShareAppMessage() {
		
	},
	/* 微信上拉加载更多 */
	onReachBottom(){
		let that=this;
		let type=that.data.type;
		let num=that.data.num;
		that.typeorder(type,num)/* 加载订单数据 */
	},
	typeorder(type,num){
		let that = this;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let paydata;
		let	_url = base_url + '/order/orderStatus';
		paydata={
			buyerOpenId:userInfo.openId,
			offset:num,
			limit:10,
			orderType:type
		}
		/* 加载列表数据 */
		that.showmyorder(type,paydata,userInfo,_url);
	},
	
	// 导航点击
	navTap(e) {
		let that = this;
		let type = e.currentTarget.dataset.type;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		that.data.num=0;
		let num=that.data.num;
		//that.data.type=orderType;
		let paydata;
		/* 支付状态 */
		
		let	_url = base_url + '/order/orderStatus';
		paydata={
			buyerOpenId:userInfo.openId,
			offset:num,
			limit:10,
			orderType:type
		}
		
		console.log(paydata);
		//return false;
		//首页所有数据
		that.showmyorder(type,paydata,userInfo,_url);
		
	},
	/* 退款 */
	drawback(e){
		let that=this;
		let index=e.currentTarget.dataset.index;
		let orderNumber=e.currentTarget.dataset.number;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/refundOrder';
		let	_url_ = base_url + '/order/orderStatus';
		let paydata={
				buyerOpenId:userInfo.openId,
				offset:0,
				limit:10,
				orderType:1
			}
		wx.showModal({
			title: "提示",
			content: "确认需要退款吗?",
			showCancel: true,
			success:function(res){
				if (res.confirm) {
				   /*订单退款函数*/
				   wx.showLoading({
				   	title: '客官不要急~',
				   	mask: true
				   });
				   wx.request({
				   	url: _url,
				   	data: {
				   		orderNumber:orderNumber,
				   		buyerOpenId:userInfo.openId
				   	},
				   	method: 'post',
				   	header: {
				   		'content-type': 'application/x-www-form-urlencoded' ,// 默认值
				   		'headerId': userInfo.openId,
				   		'headerValue': userInfo.headerValue
				   	},
				   	success: function(res) {
				   		console.log(res);
						if(res.data.status==200){
							/* 加载列表数据 */
							that.showmyorder(1,paydata,userInfo,_url_);
						}
				   		
				   	}
				   })
				} else if (res.cancel) {
					console.log('用户点击取消');
					return false;
				}
			}
		});
		
	},
	/* 取消支付 */
	canselpay(e){
		let that=this;
		let index=e.currentTarget.dataset.index;
		let orderlist=that.data.orderlist;
		let orderNumber=e.currentTarget.dataset.number;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/cancel';
		let	_url_ = base_url + '/order/orderStatus';
		let paydata={
				buyerOpenId:userInfo.openId,
				offset:0,
				limit:10,
				orderType:3/* 请求参数字段 */
			}
		wx.showModal({
			title: "提示",
			content: "确认取消订单吗?",
			showCancel: true,
			success:function(res){
				if (res.confirm) {
					wx.showLoading({
						title: '客官不要急~',
						mask: true
					});
				   /*订单支付函数*/
				   wx.request({
				   	url: _url,
				   	data: {
				   		orderNumber:orderNumber,
				   		buyerOpenId:userInfo.openId
				   	},
				   	method: 'post',
				   	header: {
				   		'content-type': 'application/x-www-form-urlencoded' ,// 默认值
				   		'headerId': userInfo.openId,
				   		'headerValue': userInfo.headerValue
				   	},
				   	success: function(res) {
				   		console.log(res);
						if(res.data.status==200){
							/* 加载列表数据 */
							/* 3为样式控制 */
							that.showmyorder(3,paydata,userInfo,_url_);
						}else{
							wx.showToast({
							  title: res.data.msg,
							  icon: 'success',
							  duration: 1000,
							})
							setTimeout(function(){
							  wx.hideToast();
							  
							},1000) 
						}
				   		
				   	}
				   })
				} else if (res.cancel) {
					console.log('用户点击取消');
					return false;
				}
			}
		});
		
	},
	/* 去支付 */
	topay(e){
		let that=this;
		that.data.payindex=e.currentTarget.dataset.index;/* 定义去支付时订单下标 */
		let paytype=1;
		let index=e.currentTarget.dataset.index;
		let orderNumber=e.currentTarget.dataset.number;
		that.data.payAmount=e.currentTarget.dataset.payamount;
		console.log(that.data.payAmount);
		let userInfo = wx.getStorageSync('userInfo');
		that.orderdetailcommon(orderNumber,userInfo,paytype)
	},
	/* 提醒发货 */
	reminddelivery(e){
		let that=this;
		let index=e.currentTarget.dataset.index;
		let orderNumber=e.currentTarget.dataset.number;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/reminde';
		wx.showLoading({
			title: '客官不要急~',
			mask: true
		})
		wx.request({
			url: _url,
			data: {
				orderNumber:orderNumber
			},
			header: {
				'content-type': 'application/x-www-form-urlencoded', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			method:'post',
			success: function(res) {
				console.log(res);
				if(res.data.status==200){
					wx.showModal({
						title: "提示",
						content: "我们会尽快发货，请耐心等候。",
						showCancel: false
					});
					that.setData({
						reminddelivery:true
					})
				}
			},
		})
	},
	/* 确认收货 */
	confirmgoods(e){
		let that=this;
		let index=e.currentTarget.dataset.index;
		let orderNumber=e.currentTarget.dataset.number;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/confirm';
		console.log(orderNumber,_url,userInfo);
		let	_url_ = base_url + '/order/orderStatus';
		let paydata={
				buyerOpenId:userInfo.openId,
				offset:0,
				limit:10,
				orderType:2
			}
		wx.showModal({
			title: "提示",
			content: "确认收货吗?",
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					/*确认收货函数*/
					wx.showLoading({
						title: '客官不要急~',
						mask: true
					})
					wx.request({
						url: _url,
						data: {
							orderNumber:orderNumber
						},
						method:'post',
						header: {
							'content-type': 'application/x-www-form-urlencoded', // 默认值
							'headerId': userInfo.openId,
							'headerValue': userInfo.headerValue
						},
						success: function(res) {
							console.log(res);
							if(res.data.status==200){
								that.showmyorder(2,paydata,userInfo,_url_);
								wx.hideLoading(); /* 隐藏加载框 */
								wx.showModal({
									title: "提示",
									content: "感谢您的购买，欢迎再次光临。",
									showCancel: false
								}); 
							}
						},
					})
					return false;
				} else if (res.cancel) {
					console.log('用户点击取消');
					return false;
				}
			}
		});	
	},
	/* 已取消板块 删除订单 */
	deleteorder(e){
		let that=this;
		let index=e.currentTarget.dataset.index;
		let orderlist=that.data.orderlist;
		let orderNumber=e.currentTarget.dataset.number;
		let userInfo = wx.getStorageSync('userInfo');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/cancel/delete';
		console.log(orderNumber,_url,userInfo);
		//return false;
		wx.showModal({
			title: "提示",
			content: "确认删除吗?",
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					/*订单支付函数*/
					wx.showLoading({
						title: '客官不要急~',
						mask: true
					})
					wx.request({
						url: _url,
						data: {
							orderNumber:orderNumber
						},
						method:'post',
						header: {
							'content-type': 'application/x-www-form-urlencoded', // 默认值
							'headerId': userInfo.openId,
							'headerValue': userInfo.headerValue
						},
						success: function(res) {
							console.log(res);
							if(res.data.status==200){
								orderlist.splice(index,1);
								that.data.orderlist=orderlist;
								that.setData({
									orderlist:orderlist
								})
								wx.hideLoading(); /* 隐藏加载框 */
								wx.showModal({
									title: "提示",
									content: "订单已删除",
									showCancel: false
								}); 
							}
						},
					})
					return false;
				} else if (res.cancel) {
					console.log('用户点击取消');
					return false;
				}
			}
		});
		
		
		
		
	},
	/* 请求数据公共函数 */
	showmyorder(type,data,userInfo,_url){
		console.log(type,data,userInfo);
		let shopimg=app.globalData.baseimg
		let that = this;
		wx.showLoading({
		  title: '客官不要急~',
		  mask:true
		})
		//首页所有数据
		wx.request({
			url: _url,
			data: data,
			method: 'post',
			header: {
				'content-type': 'application/x-www-form-urlencoded' ,// 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				if(res.data.status==200){
					let content=res.data.data.content;
					console.log(content);
					
					for (var i = 0; i < content.length; i++) {
						if(content[i].freightCharge==null){
							content[i].freightCharge=0;
						}
						if(content[i].remark==null){
							content[i].remark="无备注";
						}
						if(content[i].payStatus==0){
							content[i].payStatus=true;
						}else{
							content[i].payStatus=false;
						}
						if(content[i].deliveryStatus==null){
							content[i].deliveryStatus=0;
						}
						content[i].orderAmount=Number(content[i].orderAmount).toFixed(2);
						content[i].freightCharge=Number(content[i].freightCharge).toFixed(2);
						if(!content[i].payAmount){
							content[i].payAmount=0;
						}
						content[i].payAmount=Number(content[i].orderAmount)+Number(content[i].freightCharge);
						content[i].payAmount=Number(content[i].payAmount).toFixed(2);
					}
					
					/* 判断数据是否和上次数据一样 */
					
					
					/* that.data.num++;/* 请求页数加一 */
					/* let orderlist=content.concat(that.data.orderlist); */
					that.data.orderlist=content;
					wx.hideLoading(); /* 隐藏加载框 */
					that.setData({
						orderlist: content,
						type: type,
						shopimg:shopimg,
						payStatus:false
					})
				}
				
			}
		})
		
	},
	/* 查看列表详情 */
	orderlistdetail(e){
		let that=this;
		let paytype=0;/* 0：查看详情1；去支付 */
		let userInfo = wx.getStorageSync('userInfo');
		let index=e.currentTarget.dataset.index;
		let orderNumber=e.currentTarget.dataset.number;
		let content=that.data.orderlist;
		let liststutas=that.data.orderlist[index].liststutas;
		console.log(orderNumber,index,liststutas);
		//return false;
		if(liststutas==false||liststutas==undefined){
			liststutas=true;
			that.data.orderlist[index].liststutas=true;
			that.orderdetailcommon(orderNumber,userInfo,paytype);/* 订单列表详情 */
		}else{
			liststutas=false;
			that.data.orderlist[index].liststutas=false;
			that.setData({
				orderlist:that.data.orderlist
			})
		}
	},
	/* 查找订单详情公共函数 */
	orderdetailcommon(orderNumber,userInfo,paytype){
		let that=this;
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/order/queryDetails';
		wx.showLoading({
			title: '客官不要急~',
			mask: true
		})
		wx.request({
			url: _url,
			data: {
				orderNumber:orderNumber,
				buyerOpenId:userInfo.openId
			},
			method: 'post',
			header: {
				'content-type': 'application/x-www-form-urlencoded' ,// 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res);
				if(res.data.status==200){
					let orderItems=res.data.data;
					that.data.orderItems=orderItems;/* 列表放在全局变量中 */
					wx.hideLoading(); /* 隐藏加载框 */
					that.setData({
						orderlist:that.data.orderlist,
						orderItems:orderItems
					})
					if(paytype==0){/* 0：就是查详情1：去支付 */
					}else{
						that.confirmorder();/* 确认订单 */
					}
					
				}
			}
		})
	},
	/* 去确认订单页面 */
	confirmorder(e){
		let that=this;
		let index=that.data.payindex;
		let order=that.data.orderlist[index];
		let orderItems=that.data.orderItems;
		console.log(that.data.orderlist,order,orderItems);
		let orderlist={
			order:order,
			orderItems:orderItems
		}
		let addresdirect=true;
		wx.setStorageSync('addresdirect', addresdirect);
		wx.setStorageSync('orderlist', orderlist);
		wx.navigateTo({
			url: '../confirmorder/confirmorder'
		});
		
	}
		
})
